<template>
	<view>
		<view class="content">
			<view class="list">
				<view class="row" v-for="(row,index) in addressList" :key="index">

					<view class="center">
						<view class="name-tel">
							<view class="name">{{row.username}}</view>
							<view class="tel">{{row.phone}}</view>
							<view class="default" v-if="row.is_default">
								默认
							</view>
						</view>
						<view class="address">
							{{row.recycle_address}} {{row.address}}
						</view>
						<view class="btn-box">
							<view class="use" @tap.stop="select(row)">使用</view>
							<view class="del" @tap.stop='del(row)'>删除</view>
							<view class="bianji" @tap.stop="edit(row)">
								编辑
							</view>
						</view>
					</view>
					<view class="right">

					</view>
				</view>
			</view>
		</view>
		<view class="height"></view>
		<view class="add">
			<view class="btn" @tap="add">
				<view class="icon tianjia"></view>
				<view>添加回收地址</view>
			</view>
	<!-- 		<view class="btn wx" @click="getAddressList">
				<view class="icon weixin"></view>
				<view>授权微信地址</view>
			</view> -->
		</view>

		<view class="nodata" v-if="isEmpty">
			<image mode="widthFix" src="/static/img/nodata.png" alt="">
				<p>找不到相关数据~</p>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				isSelect: false,
				commonUrl: '',
				addressList: '',
				isEmpty: false
			};
		},
		onLoad(e) {
			if (e.type == 'select') {
				this.isSelect = true;
			}
		},
		onShow() {
			this.commonUrl = this.websiteUrl()
			uni.request({
				url: this.commonUrl + 'user-address',
				data: {},
				header: {
					'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
					'token': uni.getStorageSync("token")
				},
				method: 'GET',
				success: (res) => {
					console.log(res.data.data.list)
					this.addressList = res.data.data.list;
					if (Object.keys(res.data.data.list).length == 0) {
						this.isEmpty = true;
					} else {
						this.isEmpty = false;
					}
				}
			});
		},

		methods: {
			edit(row) {
				console.log(row)
				uni.setStorage({
					key: 'address',
					data: row,
					success() {
						uni.navigateTo({
							url: "edit?type=edit"
						})
					}
				});

			},
			add() {
				uni.navigateTo({
					url: "add"
				})
			},
			getAddressList() {
				wx.chooseAddress({
					success: (res) => {
						uni.setStorage({
							key: 'selectAddress',
							data: res,
							success() {
								uni.navigateBack({
									delta: 2
								});
							}
						})
					}
				})
			},
			select(row) {
				//是否需要返回地址(从订单确认页跳过来选收货地址)
				if (!this.isSelect) {
					return;
				}
				uni.setStorage({
					key: 'selectAddress',
					data: row,
					success() {
						uni.navigateBack();
					}
				})
			},
			del(row) {
				uni.showModal({
					title: '删除提示',
					content: '你将删除这个收货地址',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: this.commonUrl + 'user-address-remove',
								data: {
									id: row.id
								},

								header: {
									'content-type': 'application/json', //自定义请求头信息
									'token': uni.getStorageSync("token")
								},
								method: 'POST',
								success: (res) => {
									if (res.data.code == 1) {
										uni.showToast({
											title: "删除成功",
											duration: 1000,
											icon: 'none'
										});
										uni.request({
											url: this.commonUrl + 'user-address',
											data: {},
											header: {
												'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
												'token': uni.getStorageSync("token")
											},
											method: 'GET',
											success: (res) => {
												console.log(res.data.data.list)
												this.addressList = res.data.data.list;
												if (Object.keys(res.data.data.list)
													.length == 0) {
													this.isEmpty = true;
												} else {
													this.isEmpty = false;
												}
											}
										});
									} else {
										uni.showToast({
											title: res.msg,
											duration: 1000,
											icon: 'none'
										});
									}
								}
							});

						} else if (res.cancel) {
							console.log('已点击取消');
						}
					}
				});

			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #f6f6f6;
	}

	.btn-box {
		border-top: 1px solid #f1f1f1;
		padding-top: 20rpx;
		margin-top: 20rpx;
		width: 100%;

		.del,
		.bianji,
		.use {
			font-size: 24rpx;
			background-color: #f06c7a;
			color: #fff;
			padding: 20rpx 40rpx;
			border-radius: 10rpx;
			margin-right: 20rpx;
		}

		.use {
			background-color: #3cbdc5;
		}

		.bianji {
			background-color: #109cdc;
		}
	}

	.content {
		width: 100%;

		view {
			display: flex;
		}
	}

	.height {
		width: 100%;
		height: 250rpx;
	}

	.add {
		position: fixed;
		bottom: 0;
		width: 100%;

		.btn {
			width: 95%;
			height: 90upx;
			border-radius: 10upx;
			background: #3cbdc5;
			color: #fff;
			margin: 0 auto 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30upx;

			.icon {
				color: #fff;
				font-size: 36upx;
				margin-right: 15rpx;
			}

		}

		.btn.wx {
			color: #55B836;
			background: #fff;
			border: 1px solid #55B836;

			.icon {
				color: #55B836;
			}
		}
	}

	.list {
		flex-wrap: wrap;
		width: 100%;

		.row {
			width: 94%;
			padding: 20upx 3%;
			background: #fff;
			margin-bottom: 20rpx;

			.left {
				width: 90upx;
				flex-shrink: 0;
				align-items: center;

				.head {
					width: 70upx;
					height: 70upx;
					background: linear-gradient(to right, #ccc, #aaa);
					color: #fff;
					justify-content: center;
					align-items: center;
					border-radius: 60upx;
					font-size: 35upx;
				}
			}

			.center {
				width: 100%;
				flex-wrap: wrap;

				.name-tel {
					width: 100%;
					align-items: baseline;
					line-height: 45rpx;

					.name {
						font-size: 30rpx;
						font-weight: bold;
						color: #333;
					}

					.tel {
						margin-left: 30upx;
						font-size: 26rpx;
						font-weight: bold;
						color: #333;
					}

					.default {
						border: 1px solid #3cbdc5;
						padding: 0 20rpx;
						color: #3cbdc5;
						font-size: 20rpx;
						margin-left: 20rpx;
						border-radius: 10rpx;
					}

				}

				.address {
					width: 100%;
					font-size: 24upx;
					align-items: baseline;
					color: #777;
				}
			}

			.right {
				flex-shrink: 0;
				align-items: center;
				margin-left: 20upx;

				.icon {
					justify-content: center;
					align-items: center;
					width: 75upx;
					height: 60upx;
					border-left: solid 1upx #f1f1f1;
					font-size: 32upx;
					color: #777;
				}
			}
		}
	}
</style>